home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************************
- Align.h
- <http://redshed.net/align>
-
- Straighten up and fly right
- Straighten up and fly right
- Straighten up and fly right
- Cool down papa, don't you blow your top!
-
- Copyright © 1999-2001 Red Shed Software. All rights reserved.
- by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright notice, this
- list of conditions and the following disclaimer in the documentation and/or other
- materials provided with the distribution.
-
- 3. The names and trademarks of copyright holders may not be used in advertising
- or publicity pertaining to the software without specific prior permission.
-
- This software is provided "as is" and any expressed or implied warranties,
- including, but not limited to, the implied warranties of merchantability and
- fitness for a particular purpose are disclaimed. In no event shall the authors
- or copyright holders be liable for any direct, indirect, incidental, special,
- exemplary, or consequential damages (including, but not limited to, procurement
- of substitute goods or services; loss of use, data, or profits; or business
- interruption) however caused and on any theory of liability, whether in contract,
- strict liability, or tort (including negligence or otherwise) arising in any way
- out of the use of this software, even if advised of the possibility of such
- damage.
-
- Commenter Date Comment
- --------- ----------------- -----------------------------------------------------
- wolf Fri, Feb 12, 1999 Created.
- wolf Thu, Apr 1, 1999 Added Aligned template.
- wolf Mon, Apr 12, 1999 Added AlignX().
- Rewrote Aligned template. Now supports 2, 8 and 16
- byte-aligned data as well as 4.
- wolf Tue, Apr 13, 1999 Rewrote NewAlignedPtr() to take alignment as a
- literal variable instead of a flag. This makes it
- more symmetrical.
- wolf Fri, May 7, 1999 The Aligned template worked well for C data
- structures but contained a flaw for C++ constructed
- objects. The Constructor would be called on the
- non-aligned pointer and then the pointer would be
- aligned (moved). The result would that the
- Constructor operated on the wrong section of memory.
-
- I now align the pointer *and then* call "placement
- new" to call the Constructor on the correct section.
-
- The old Aligned template is still included under the
- name OldAligned.
-
- Broke out Aligned and OldAligned templates into
- their own header file, Aligned.h.
- wolf Mon, Nov 1, 1999 Added AlignDirect_(), which is functionally
- identical to Align_() (where _ is either 2, 4, 8 or
- 16) except it takes a pointer as its parameter and
- returns a pointer. This is unlike Align_(), which
- takes a pointer to a pointer and returns nothing.
- AlignDirect_() makes it possible to declare a
- variable with an initial value
- ("int *x = (int*) AlignDirect_( &alignedX );").
- Used by the new ALIGN macro. By the way,
- AlignDirect_() calls Align_() to do its work.
-
- Added a new macro: ALIGN. It works much like the
- Aligned template except it's a macro, so it works
- under C as well as C++.
- wolf Sat, Jan 22, 2000 Rewrote AlignX() to be faster.
- wolf Sun, Jan 23, 2000 Added Pad typedef.
- wolf Sun, Jan 30, 2000 Carbonized.
- wolf Fri, Feb 18, 2000 Added IsAligned().
- wolf Mon, Dec 11, 2000 Align 1.0.
- wolf Thu, Feb 8, 2001 Align 1.0.2. CodeWarrior Pro 6 breaks an idiom:
- implicit casting of <type>** to void**. I had to make
- all such casts explicit.
-
- ************************************************************************************/
-
- #ifndef _Align_
- #define _Align_
-
- /**************************
- *
- * Types
- *
- **************************/
- #pragma mark (Types)
-
- typedef UInt8 Pad;
- typedef UInt16 Pad2;
- typedef UInt32 Pad4;
- typedef UInt32 Pad8[ 2 ];
- typedef UInt32 Pad16[ 4 ];
-
- /**************************
- *
- * Interfaces
- *
- **************************/
- #pragma mark -
- #pragma mark (Interfaces)
-
- #ifdef __cplusplus
- extern "C" {
- #endif // __cplusplus
-
- extern
- void
- Align2(
- void **ptr );
-
- extern
- void*
- AlignDirect2(
- void *ptr );
-
- extern
- void
- Align4(
- void **ptr );
-
- extern
- void*
- AlignDirect4(
- void *ptr );
-
- extern
- void
- Align8(
- void **ptr );
-
- extern
- void*
- AlignDirect8(
- void *ptr );
-
- extern
- void
- Align16(
- void **ptr );
-
- extern
- void*
- AlignDirect16(
- void *ptr );
-
- extern
- void
- AlignX(
- void **ptr,
- UInt32 alignment );
-
- extern
- void*
- AlignXDirect(
- void *ptr,
- UInt32 alignment );
-
- extern
- OSErr
- NewAlignedPtr(
- UInt32 size,
- Boolean fromSystemHeap,
- Boolean clearMemory,
- UInt8 alignment,
- void **ptr );
-
- extern
- Ptr
- GetUnalignedPtr(
- void *ptr );
-
- extern
- Boolean
- IsAligned(
- void *ptr,
- UInt8 alignment );
-
- #ifdef __cplusplus
- }
- #endif
-
- /**************************
- *
- * DeclareAlignedVariable Macro
- *
- **************************/
- #pragma mark -
- #pragma mark (DeclareAlignedVariable Macro)
-
- #ifdef __cplusplus
- #include <new>
- #define DeclareAlignedVariable( TYPE, NAME, ALIGNMENT ) \
- struct Aligned##TYPE##NAME { \
- Pad padding[ ALIGNMENT ]; \
- char value[sizeof(TYPE)]; \
- }; \
- struct Aligned##TYPE##NAME aligned##NAME; \
- TYPE *NAME = (TYPE*) AlignXDirect( &aligned##NAME, ALIGNMENT ); \
- NAME = new ( NAME ) TYPE
- #else
- #define DeclareAlignedVariable( TYPE, NAME, ALIGNMENT ) \
- struct Aligned##TYPE##NAME { \
- Pad padding[ ALIGNMENT ]; \
- TYPE value; \
- }; \
- struct Aligned##TYPE##NAME aligned##NAME; \
- TYPE *NAME = (TYPE*) AlignXDirect( &aligned##NAME, ALIGNMENT )
- #endif
-
- /**************************
- *
- * ALIGN macro
- *
- **************************/
- #pragma mark -
- #pragma mark (ALIGN macro)
-
- #ifdef __cplusplus
- #include <new>
- #define ALIGN( TYPE, NAME, ALIGNMENT ) \
- struct Aligned##TYPE { \
- Pad##ALIGNMENT padding; \
- char value[sizeof(TYPE)]; \
- }; \
- struct Aligned##TYPE aligned##NAME; \
- TYPE *NAME = (TYPE*) AlignDirect##ALIGNMENT( &aligned##NAME ); \
- NAME = new ( NAME ) TYPE
- #else
- #define ALIGN( TYPE, NAME, ALIGNMENT ) \
- struct Aligned##TYPE { \
- Pad##ALIGNMENT padding; \
- TYPE value; \
- }; \
- struct Aligned##TYPE aligned##NAME; \
- TYPE *NAME = (TYPE*) AlignDirect##ALIGNMENT( &aligned##NAME )
- #endif
-
- #endif // _Align_